home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / perl5 / Compress / Bzip2.pm
Text File  |  2008-12-05  |  39KB  |  1,560 lines

  1. # File       : Bzip2.pm
  2. # Author   : Rob Janes
  3. # Created  : 14 April 2005
  4. # Modified : 9 Aug 2005
  5. # Version  : 2.09
  6. #
  7. #     Copyright (c) 2005 Rob Janes. All rights reserved.
  8. #     This program is free software; you can redistribute it and/or
  9. #     modify it under the same terms as Perl itself.
  10. #
  11.  
  12. package Compress::Bzip2;
  13.  
  14. use 5.006;
  15.  
  16. use strict;
  17. use warnings;
  18.  
  19. use Carp;
  20. use Getopt::Std;
  21. use Fcntl qw(:DEFAULT :mode);
  22.  
  23. require Exporter;
  24. use AutoLoader;
  25.  
  26. our @ISA = qw(Exporter);
  27.  
  28. # Items to export into callers namespace by default. Note: do not export
  29. # names by default without a very good reason. Use EXPORT_OK instead.
  30. # Do not simply export all your public functions/methods/constants.
  31.  
  32. # This allows declaration    use Compress::Bzip2 ':all';
  33. # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
  34. # will save memory.
  35. our %EXPORT_TAGS =
  36.     ( 'constants' => [ qw(
  37.               BZ_CONFIG_ERROR
  38.               BZ_DATA_ERROR
  39.               BZ_DATA_ERROR_MAGIC
  40.               BZ_FINISH
  41.               BZ_FINISH_OK
  42.               BZ_FLUSH
  43.               BZ_FLUSH_OK
  44.               BZ_IO_ERROR
  45.               BZ_MAX_UNUSED
  46.               BZ_MEM_ERROR
  47.               BZ_OK
  48.               BZ_OUTBUFF_FULL
  49.               BZ_PARAM_ERROR
  50.               BZ_RUN
  51.               BZ_RUN_OK
  52.               BZ_SEQUENCE_ERROR
  53.               BZ_STREAM_END
  54.               BZ_UNEXPECTED_EOF
  55.              ) ],
  56.  
  57.       'utilities' => [ qw(
  58.               &bzopen
  59.               &bzinflateInit
  60.               &bzdeflateInit
  61.               &memBzip &memBunzip
  62.               &compress &decompress
  63.               &bzip2 &bunzip2
  64.               &bzlibversion
  65.               $bzerrno
  66.               ) ],
  67.  
  68.       'bzip1' => [ qw(
  69.               &compress
  70.               &decompress
  71.               &compress_init
  72.               &decompress_init
  73.               &version
  74.               ) ],
  75.  
  76.       'gzip' => [ qw(
  77.              &gzopen
  78.              &inflateInit
  79.              &deflateInit
  80.              &compress &uncompress
  81.              &adler32 &crc32
  82.  
  83.              ZLIB_VERSION
  84.  
  85.              $gzerrno
  86.  
  87.              Z_OK
  88.              Z_STREAM_END
  89.              Z_NEED_DICT
  90.              Z_ERRNO
  91.              Z_STREAM_ERROR
  92.              Z_DATA_ERROR
  93.              Z_MEM_ERROR
  94.              Z_BUF_ERROR
  95.              Z_VERSION_ERROR
  96.  
  97.              Z_NO_FLUSH
  98.              Z_PARTIAL_FLUSH
  99.              Z_SYNC_FLUSH
  100.              Z_FULL_FLUSH
  101.              Z_FINISH
  102.              Z_BLOCK
  103.  
  104.              Z_NO_COMPRESSION
  105.              Z_BEST_SPEED
  106.              Z_BEST_COMPRESSION
  107.              Z_DEFAULT_COMPRESSION
  108.  
  109.              Z_FILTERED
  110.              Z_HUFFMAN_ONLY
  111.              Z_RLE
  112.              Z_DEFAULT_STRATEGY
  113.  
  114.              Z_BINARY
  115.              Z_ASCII
  116.              Z_UNKNOWN
  117.  
  118.              Z_DEFLATED
  119.              Z_NULL
  120.              ) ],
  121.       );
  122.  
  123. our @EXPORT_OK = ( @{ $EXPORT_TAGS{'utilities'} },
  124.            @{ $EXPORT_TAGS{'constants'} },
  125.            @{ $EXPORT_TAGS{'bzip1'} },
  126.            @{ $EXPORT_TAGS{'gzip'} },
  127.            );
  128.  
  129. $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
  130.  
  131. our @EXPORT = ( @{ $EXPORT_TAGS{'utilities'} }, @{ $EXPORT_TAGS{'constants'} } );
  132.  
  133. our $VERSION = "2.09";
  134.  
  135. our $bzerrno = "";
  136. our $gzerrno;
  137. *gzerrno = \$bzerrno;
  138.  
  139. # Zlib compatibility
  140. ##
  141. use constant ZLIB_VERSION => '1.x';
  142. # allowed flush values
  143. use constant { Z_NO_FLUSH => 0, Z_PARTIAL_FLUSH => 1, Z_SYNC_FLUSH => 2,
  144.            Z_FULL_FLUSH => 3, Z_FINISH => 4, Z_BLOCK => 5 };
  145. # return codes for functions, positive normal, negative error
  146. use constant { Z_OK => 0, Z_STREAM_END => 1, Z_NEED_DICT => 2, Z_ERRNO => -1,
  147.            Z_STREAM_ERROR => -2, Z_DATA_ERROR => -3, Z_MEM_ERROR => -4,
  148.            Z_BUF_ERROR => -5, Z_VERSION_ERROR => -6 };
  149. # compression levels
  150. use constant { Z_NO_COMPRESSION => 0, Z_BEST_SPEED => 1,
  151.            Z_BEST_COMPRESSION => 9, Z_DEFAULT_COMPRESSION => -1 };
  152. # compression strategy, for deflateInit
  153. use constant { Z_FILTERED => 1, Z_HUFFMAN_ONLY => 2, Z_RLE => 3,
  154.            Z_DEFAULT_STRATEGY => 0 };
  155. # possible values of data_type (inflate)
  156. use constant { Z_BINARY => 0, Z_ASCII => 1, Z_UNKNOWN => 2 };
  157. # the deflate compression method
  158. use constant Z_DEFLATED => 8;
  159. # for initialization
  160. use constant Z_NULL => 0;
  161.  
  162. ## gzopen, $gzerror, gzerror, gzclose, gzreadline, gzwrite
  163.  
  164. sub AUTOLOAD {
  165.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  166.     # XS function.
  167.  
  168.     my $constname;
  169.     our $AUTOLOAD;
  170.     ($constname = $AUTOLOAD) =~ s/.*:://;
  171.     croak "&Compress::Bzip2::constant not defined" if $constname eq 'constant';
  172.     my ($error, $val) = constant($constname);
  173.     if ($error) { croak $error; }
  174.     {
  175.     no strict 'refs';
  176.     # Fixed between 5.005_53 and 5.005_61
  177. #XXX    if ($] >= 5.00561) {
  178. #XXX        *$AUTOLOAD = sub () { $val };
  179. #XXX    }
  180. #XXX    else {
  181.         *$AUTOLOAD = sub { $val };
  182. #XXX    }
  183.     }
  184.     goto &$AUTOLOAD;
  185. }
  186.  
  187. require XSLoader;
  188. XSLoader::load('Compress::Bzip2', $VERSION);
  189.  
  190. #bootstrap Compress::Bzip2 $VERSION;
  191.  
  192. ##############################################################################
  193. ## file compress uncompress commands
  194.  
  195. sub _writefileopen ( $$;$ ) {
  196.   ## open a protected file for write
  197.   my ( $handle, $filename, $force ) = @_;
  198.  
  199.   if ( sysopen($handle, $filename, $force ? O_WRONLY|O_CREAT|O_TRUNC : O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR) ) {
  200.     $_[0] = $handle if !defined($_[0]);
  201.     return $handle;
  202.   }
  203.  
  204.   return undef;
  205. }
  206.  
  207. sub _stat_snapshot ( $ ) {
  208.   my ( $filename ) = @_;
  209.   return undef if !defined($filename);
  210.  
  211.   my @stats = stat $filename;
  212.   if (!@stats) {
  213.     warn "stat of $filename failed: $!\n" if !@stats;
  214.     return undef;
  215.   }
  216.  
  217.   return \@stats;
  218. }
  219.  
  220. sub _check_stat ( $$;$ ) {
  221.   my ( $filename, $statsnap, $force ) = @_;
  222.  
  223.   if ( !defined($statsnap) || (ref($statsnap) eq 'ARRAY' && @$statsnap == 0) ) {
  224.     $statsnap = _stat_snapshot( $filename );
  225.     if ( $statsnap ) {
  226.       if ( @_>1 ) {
  227.     if ( !defined($_[1]) ) {
  228.       $_[1] = $statsnap;
  229.     }
  230.     elsif ( ref($_[1]) eq 'ARRAY' && @{ $_[1] } == 0 ) {
  231.       @{ $_[1] } = @$statsnap;
  232.     }
  233.       }
  234.     }
  235.     else {
  236.       return undef;
  237.     }
  238.   }
  239.  
  240.   if ( S_ISDIR( $statsnap->[2] ) ) {
  241.     bz_seterror( &BZ_IO_ERROR, "file $filename is a directory" );
  242.     return 0;
  243.   }
  244.  
  245.   if ( !S_ISREG( $statsnap->[2] ) ) {
  246.     bz_seterror( &BZ_IO_ERROR, "file $filename is not a normal file" );
  247.     return 0;
  248.   }
  249.  
  250.   if ( !$force && S_ISLNK( $statsnap->[2] ) ) {
  251.     bz_seterror( &BZ_IO_ERROR, "file $filename is a symlink" );
  252.     return 0;
  253.   }
  254.  
  255.   if ( !$force && $statsnap->[3] > 1 ) {
  256.     bz_seterror( &BZ_IO_ERROR, "file $filename has too many hard links" );
  257.     return 0;
  258.   }
  259.  
  260.   return 1;
  261. }
  262.  
  263. sub _set_stat_from_snapshot ( $$ ) {
  264.   my ( $filename, $statsnap ) = @_;
  265.  
  266.   if ( !chmod( S_IMODE( $statsnap->[2] ), $filename ) ) {
  267.     bz_seterror( &BZ_IO_ERROR, "chmod ".sprintf('%03o', S_IMODE( $statsnap->[2] ))." $filename failed: $!" );
  268.     return undef;
  269.   }
  270.  
  271.   if ( !utime @$statsnap[8,9], $filename ) {
  272.     bz_seterror( &BZ_IO_ERROR,
  273.          "utime " . join(' ',map { strftime('%Y-%m-%d %H:%M:%S', localtime $_) } @$statsnap[8,9] ) .
  274.          " $filename failed: $!" );
  275.     return undef;
  276.   }
  277.  
  278.   if ( !chown @$statsnap[4,5], $filename ) {
  279.     bz_seterror( &BZ_IO_ERROR,
  280.          "chown " . join(':', ( getpwuid($statsnap->[4]) )[0], ( getgrgid($statsnap->[5]) )[0]) .
  281.          " $filename failed: $!" );
  282.     return 0;
  283.   }
  284.  
  285.   return 1;
  286. }
  287.  
  288. sub bzip2 ( @ ) {
  289.   return _process_files( 'bzip2', 'cfvks123456789', @_ );
  290. }
  291.  
  292. sub bunzip2 ( @ ) {
  293.   return _process_files( 'bunzip2', 'cdzfks123456789', @_ );
  294. }
  295.  
  296. sub bzcat ( @ ) {
  297.   return _process_files( 'bzcat', 'cdzfks123456789', @_ );
  298. }
  299.  
  300. sub _process_files ( @ ) {
  301.   my $command = shift;
  302.   my $opts = shift;
  303.  
  304.   local @ARGV = @_;
  305.  
  306.   my %opts;
  307.   return undef if !getopt( $opts, \%opts );
  308.   # c compress or decompress to stdout
  309.   # d decompress
  310.   # z compress
  311.   # f force
  312.   # v verbose
  313.   # k keep
  314.   # s small
  315.   # 123456789
  316.  
  317.   $opts{c} = 1 if $command eq 'bzcat';
  318.   $opts{d} = 1 if $command eq 'bunzip2' || $command eq 'bzcat';
  319.   $opts{z} = 1 if $command eq 'bzip2';
  320.  
  321.   my $read_from_stdin;
  322.   my ( $in, $bzin );
  323.   my ( $out, $bzout );
  324.  
  325.   if ( !@ARGV ) {
  326.     $read_from_stdin = 1;
  327.     $opts{c} = 1;
  328.     if ( !open( $in, "<&STDIN" ) ) {
  329.       die "Error: failed to input from STDIN: '$!'\n";
  330.     }
  331.  
  332.     $bzin = bzopen( $in, "r" );
  333.   }
  334.  
  335.   if ( $opts{c} ) {
  336.     if ( !open( $out, ">&STDOUT" ) ) {
  337.       die "Error: failed to output to STDOUT: '$!'\n";
  338.     }
  339.  
  340.     $bzout = bzopen( $out, "w" );
  341.   }
  342.  
  343.   if ( !$opts{d} && !$opts{z} ) {
  344.     die "Error: neither compress nor decompress was indicated.\n";
  345.   }
  346.  
  347.   my $doneflag = 0;
  348.   while ( !$doneflag ) {
  349.     my $infile;
  350.     my $outfile;
  351.     my @statbuf;
  352.  
  353.     if ( !$read_from_stdin ) {
  354.       $infile = shift @ARGV;
  355.       if ( ! -r $infile ) {
  356.     print STDERR "Error: file $infile is not readable\n";
  357.     next;
  358.       }
  359.  
  360.       @statbuf = stat _;
  361.       if ( !@statbuf ) {
  362.     print STDERR "Error: failed to stat $infile: '$!'\n";
  363.     next;
  364.       }
  365.       
  366.       if ( !_check_stat( $infile, \@statbuf, $opts{f} ) ) {
  367.     print STDERR "Error: file $infile stat check fails: $bzerrno\n";
  368.     next;
  369.       }
  370.     }
  371.  
  372.     my $outfile_exists;
  373.     if ( !$opts{c} ) {
  374.       undef $out;
  375.       if ( $opts{d} ) {
  376.     $outfile = $infile . '.bz2';
  377.       }
  378.       elsif ( $opts{z} ) {
  379.     $outfile = $infile =~ /\.bz2$/ ? substr($infile,0,-4) : $infile.'.out';
  380.       }
  381.  
  382.       $outfile_exists = -e $outfile;
  383.       if ( !_writefileopen( $out, $outfile, $opts{f} ) ) {
  384.     print STDERR "Error: failed to open $outfile for write: '$!'\n";
  385.     next;
  386.       }
  387.     }
  388.     
  389.     if ( !$read_from_stdin ) {
  390.       undef $in;
  391.       if ( !open( $in, $infile ) ) {
  392.     print STDERR "Error: unable to open $infile: '$!'\n";
  393.     unlink( $outfile ) if !$outfile_exists;
  394.     next;
  395.       }
  396.     }
  397.  
  398.     if ( $opts{d} ) {
  399.       $bzin = bzopen( $in, "r" ) if !$read_from_stdin;
  400.  
  401.       my $buf;
  402.       my $notdone = 1;
  403.       while ( $notdone ) {
  404.     my $ln = bzread( $in, $buf, 1024 );
  405.     if ( $ln > 0 ) {
  406.       syswrite( $out, $buf, $ln );
  407.     }
  408.     elsif ( $ln == 0 ) {
  409.       undef $notdone;
  410.     }
  411.     else {
  412.     }
  413.       }
  414.  
  415.       close($out);
  416.  
  417.       if ( !$read_from_stdin ) {
  418.     bzclose($in);
  419.     unlink( $infile ) if !$opts{k};
  420.     _set_stat_from_snapshot( $outfile, \@statbuf );
  421.       }
  422.     }
  423.     elsif ( $opts{z} ) {
  424.       $bzout = bzopen( $out, "w" ) if !$opts{c};
  425.  
  426.       my $buf;
  427.       my $notdone = 1;
  428.       while ( $notdone ) {
  429.     my $ln = sysread( $in, $buf, 1024 );
  430.     if ( $ln > 0 ) {
  431.       bzwrite( $bzout, $buf, $ln );
  432.     }
  433.     elsif ( $ln == 0 ) {
  434.       undef $notdone;
  435.     }
  436.     else {
  437.     }
  438.       }
  439.  
  440.       close($in);
  441.  
  442.       if ( !$opts{c} ) {
  443.     bzclose($bzout);
  444.     unlink( $infile ) if !$opts{k};
  445.     _set_stat_from_snapshot( $outfile, \@statbuf );
  446.       }
  447.     }
  448.   }
  449. }
  450.   
  451. ##############################################################################
  452. ##############################################################################
  453. ## compatibility with Compress::Bzip2 1.03
  454.  
  455. sub add ( $$ ) {
  456.   my ( $obj, $buffer ) = @_;
  457.  
  458.   my @res = $obj->is_write ? $obj->bzdeflate( $buffer ) : $obj->bzinflate( $buffer );
  459.  
  460.   return $res[0];
  461. }
  462.  
  463. sub finish ( $;$ ) {
  464.   my ( $obj, $buffer ) = @_;
  465.   my ( @res, $out );
  466.  
  467.   if ( defined($buffer) ) {
  468.     @res = $obj->is_write ? $obj->bzdeflate( $buffer ) : $obj->bzinflate( $buffer );
  469.     return undef if $res[1] != &BZ_OK;
  470.  
  471.     $out = $res[0];
  472.   }
  473.   $out = '' if !defined($out);
  474.  
  475.   @res = $obj->bzclose;
  476.   return undef if $res[1] != &BZ_OK;
  477.  
  478.   return $out.$res[0];
  479. }
  480.  
  481. sub input_size ( $ ) {
  482.   my ( $obj ) = @_;
  483.   return $obj->total_in;
  484. }
  485.  
  486. sub output_size ( $ ) {
  487.   my ( $obj ) = @_;
  488.   return $obj->total_out;
  489. }
  490.  
  491. sub version ( ) {
  492.   return bzlibversion();
  493. }
  494.  
  495. sub error ( $ ) {
  496.   return $_[0]->bzerror;
  497. }
  498.  
  499. ##############################################################################
  500. ##############################################################################
  501. ## THE Compress::Zlib compatibility section
  502.  
  503. sub _bzerror2gzerror {
  504.   my ( $bz_error_num ) = @_;
  505.   my $gz_error_num =
  506.       $bz_error_num == &BZ_OK ? Z_OK :
  507.       $bz_error_num == &BZ_RUN_OK ? Z_OK :
  508.       $bz_error_num == &BZ_FLUSH_OK ? Z_STREAM_END :
  509.       $bz_error_num == &BZ_FINISH_OK ? Z_STREAM_END :
  510.       $bz_error_num == &BZ_STREAM_END ? Z_STREAM_END :
  511.  
  512.       $bz_error_num == &BZ_SEQUENCE_ERROR ? Z_VERSION_ERROR :
  513.       $bz_error_num == &BZ_PARAM_ERROR ? Z_ERRNO :
  514.       $bz_error_num == &BZ_MEM_ERROR ? Z_MEM_ERROR :
  515.       $bz_error_num == &BZ_DATA_ERROR ? Z_DATA_ERROR :
  516.       $bz_error_num == &BZ_DATA_ERROR_MAGIC ? Z_DATA_ERROR :
  517.       $bz_error_num == &BZ_IO_ERROR ? Z_ERRNO :
  518.       $bz_error_num == &BZ_UNEXPECTED_EOF ? Z_STREAM_ERROR :
  519.       $bz_error_num == &BZ_OUTBUFF_FULL ? Z_BUF_ERROR :
  520.       $bz_error_num == &BZ_CONFIG_ERROR ? Z_VERSION_ERROR :
  521.       Z_VERSION_ERROR
  522.       ;
  523.  
  524.   return $gz_error_num;
  525. }
  526.  
  527. sub gzopen ( $$ ) {
  528.   goto &bzopen;
  529. }
  530.  
  531. sub gzread ( $$;$ ) {
  532.   goto &bzread;
  533. }
  534.  
  535. sub gzreadline ( $$ ) {
  536.   goto &bzreadline;
  537. }
  538.  
  539. sub gzwrite ( $$ ) {
  540.   goto &bzwrite;
  541. }
  542.  
  543. sub gzflush ( $;$ ) {
  544.   my ( $obj, $flush ) = @_;
  545.   return Z_OK if $flush == Z_NO_FLUSH;
  546.   goto &bzflush;
  547. }
  548.  
  549. sub gzclose ( $ ) {
  550.   goto &bzclose;
  551. }
  552.  
  553. sub gzeof ( $ ) {
  554.   goto &bzeof;
  555. }
  556.  
  557. sub gzsetparams ( $$$ ) {
  558.   ## ignore params
  559.   my ( $obj, $level, $strategy ) = @_;
  560.   return Z_OK;
  561. }
  562.  
  563. sub gzerror ( $ ) {
  564.   goto &bzerror;
  565. }
  566.  
  567. sub deflateInit ( @ ) {
  568.   ## ignore all options:
  569.   ## -Level, -Method, -WindowBits, -MemLevel, -Strategy, -Dictionary, -Bufsize
  570.  
  571.   my @res = bzdeflateInit();
  572.   return $res[0] if !wantarray;
  573.  
  574.   return ( $res[0], _bzerror2gzerror( $res[1] ) );
  575. }
  576.  
  577. sub deflate ( $$ ) {
  578.   my ( $obj, $buffer ) = @_;
  579.  
  580.   my @res = $obj->bzdeflate( $buffer );
  581.  
  582.   return $res[0] if !wantarray;
  583.   return ( $res[0], _bzerror2gzerror( $res[1] ) );
  584. }
  585.  
  586. sub deflateParams ( $;@ ) {
  587.   ## ignore all options
  588.   return Z_OK;
  589. }
  590.  
  591. sub flush ( $;$ ) {
  592.   my ( $obj, $flush_type ) = @_;
  593.  
  594.   $flush_type = Z_FINISH if !defined($flush_type);
  595.   return Z_OK if $flush_type == Z_NO_FLUSH;
  596.  
  597.   my $bz_flush_type;
  598.   my @res;
  599.  
  600.   $bz_flush_type =
  601.       $flush_type == Z_PARTIAL_FLUSH || $flush_type == Z_SYNC_FLUSH ? &BZ_FLUSH :
  602.       $flush_type == Z_FULL_FLUSH ? &BZ_FINISH :
  603.       &BZ_FINISH;
  604.  
  605.   @res = $obj->bzflush( $bz_flush_type );
  606.  
  607.   return $res[0] if !wantarray;
  608.   return ( $res[0], _bzerror2gzerror( $res[1] ) );
  609. }
  610.  
  611. sub dict_adler ( $ ) {
  612.   return 1;            # ???
  613. }
  614.  
  615. sub msg ( $ ) {
  616.   my ( $obj ) = @_;
  617.  
  618.   return ''.($obj->bzerror).'';    # stringify
  619. }
  620.  
  621. sub inflateInit ( @ ) {
  622.   ## ignore all options:
  623.   ## -WindowBits, -Dictionary, -Bufsize
  624.  
  625.   my @res = bzinflateInit();
  626.   return $res[0] if !wantarray;
  627.  
  628.   return ( $res[0], _bzerror2gzerror( $res[1] ) );
  629. }
  630.  
  631. sub inflate ( $$ ) {
  632.   my ( $obj, $buffer ) = @_;
  633.  
  634.   my @res = $obj->bzinflate( $buffer );
  635.  
  636.   return $res[0] if !wantarray;
  637.   return ( $res[0], _bzerror2gzerror( $res[1] ) );
  638. }
  639.  
  640. sub inflateSync ( $ ) {
  641.   return Z_VERSION_ERROR;    # ?? what
  642. }
  643.  
  644. sub memGzip ( $ ) {
  645.   goto &memBzip;
  646. }
  647.  
  648. sub memGunzip ( $ ) {
  649.   goto &memBunzip;
  650. }
  651.  
  652. sub adler32 ( $;$ ) {
  653.   return 0;
  654. }
  655.  
  656. sub crc32 ( $;$ ) {
  657.   return 0;
  658. }
  659.  
  660. # sub compress ( $;$ ) {
  661. #   ## ignore $level
  662. #   my ( $source, $level ) = @_;
  663. #   return memBzip( $source );
  664. # }
  665.  
  666. sub uncompress ( $ ) {
  667.   my ( $source, $level ) = @_;
  668.   return memBunzip( $source );
  669. }
  670.  
  671. # Autoload methods go after =cut, and are processed by the autosplit program.
  672.  
  673. 1;
  674.  
  675. __END__
  676.  
  677. =pod
  678.  
  679. =head1 NAME
  680.  
  681. Compress::Bzip2 - Interface to Bzip2 compression library
  682.  
  683. =head1 SYNOPSIS
  684.  
  685.     use Compress::Bzip2 qw(:all :constant :utilities :gzip);
  686.  
  687.     ($bz, $status) = bzdeflateInit( [PARAMS] ) ;
  688.     ($out, $status) = $bz->bzdeflate($buffer) ;
  689.  
  690.     ($bz, $status) = bzinflateInit( [PARAMS] ) ;
  691.     ($out, $status) = $bz->bzinflate($buffer) ;
  692.  
  693.     ($out, $status) = $bz->bzflush() ;
  694.     ($out, $status) = $bz->bzclose() ;
  695.  
  696.     $dest = memBzip($source);
  697.         alias compress
  698.     $dest = memBunzip($source);
  699.         alias decompress
  700.  
  701.     $bz = Compress::Bzip2->new( [PARAMS] );
  702.  
  703.     $bz = bzopen($filename or filehandle, $mode);
  704.         alternate, with $bz created by new():
  705.     $bz->bzopen($filename or filehandle, $mode);
  706.  
  707.     $bytesread = $bz->bzread($buffer [,$size]) ;
  708.     $bytesread = $bz->bzreadline($line);
  709.     $byteswritten = $bz->bzwrite($buffer [,$limit]);
  710.     $errstring = $bz->bzerror(); 
  711.     $status = $bz->bzeof();
  712.     $status = $bz->bzflush();
  713.     $status = $bz->bzclose() ;
  714.  
  715.     $status = $bz->bzsetparams( $param => $setting );
  716.  
  717.     $bz->total_in() ;
  718.     $bz->total_out() ;
  719.  
  720.     $verstring = $bz->bzversion();
  721.  
  722.     $Compress::Bzip2::bzerrno
  723.  
  724. =head1 DESCRIPTION
  725.  
  726. The I<Compress::Bzip2> module provides a Perl interface to the I<Bzip2>
  727. compression library (see L</AUTHOR> for details about where to get
  728. I<Bzip2>). A relevant subset of the functionality provided by I<Bzip2>
  729. is available in I<Compress::Bzip2>.
  730.  
  731. All string parameters can either be a scalar or a scalar reference.
  732.  
  733. The module can be split into two general areas of functionality, namely
  734. in-memory compression/decompression and read/write access to I<bzip2>
  735. files. Each of these areas will be discussed separately below.
  736.  
  737. =head1 FILE READ/WRITE INTERFACE
  738.  
  739. A number of functions are supplied in I<bzlib> for reading and writing
  740. I<bzip2> files. Unfortunately, most of them are not suitable.  So, this
  741. module provides another interface, built over top of the low level bzlib
  742. methods.
  743.  
  744. =head2 B<$bz = bzopen(filename or filehandle, mode)>
  745.  
  746. This function returns an object which is used to access the other
  747. I<bzip2> methods.
  748.  
  749. The B<mode> parameter is used to specify both whether the file is
  750. opened for reading or writing, with "r" or "w" respectively.
  751.  
  752. If a reference to an open filehandle is passed in place of the
  753. filename, it better be positioned to the start of a
  754. compression/decompression sequence.
  755.  
  756. =head2 B<$bz = Compress::Bzip2-E<gt>new( [PARAMS] )>
  757.  
  758. Create a Compress::Bzip2 object.  Optionally, provide
  759. compression/decompression parameters as a keyword => setting list.
  760. See I<bzsetparams()> for a description of the parameters.
  761.  
  762. =head2 B<$bz-E<gt>bzopen(filename or filehandle, mode)>
  763.  
  764. This is bzopen, but it uses an object previously created by the new
  765. method.  Other than that, it is identical to the above bzopen.
  766.  
  767. =head2 B<$bytesread = $bz-E<gt>bzread($buffer [, $size]) ;>
  768.  
  769. Reads B<$size> bytes from the compressed file into B<$buffer>. If
  770. B<$size> is not specified, it will default to 4096. If the scalar
  771. B<$buffer> is not large enough, it will be extended automatically.
  772.  
  773. Returns the number of bytes actually read. On EOF it returns 0 and in
  774. the case of an error, -1.
  775.  
  776. =head2 B<$bytesread = $bz-E<gt>bzreadline($line) ;>
  777.  
  778. Reads the next line from the compressed file into B<$line>. 
  779.  
  780. Returns the number of bytes actually read. On EOF it returns 0 and in
  781. the case of an error, -1.
  782.  
  783. It IS legal to intermix calls to B<bzread> and B<bzreadline>.
  784.  
  785. At this time B<bzreadline> ignores the variable C<$/>
  786. (C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use). The
  787. end of a line is denoted by the C character C<'\n'>.
  788.  
  789. =head2 B<$byteswritten = $bz-E<gt>bzwrite($buffer [, $limit]) ;>
  790.  
  791. Writes the contents of B<$buffer> to the compressed file. Returns the
  792. number of bytes actually written, or 0 on error.
  793.  
  794. If $limit is given and non-zero, then only that many bytes from
  795. $buffer will be written.
  796.  
  797. =head2 B<$status = $bz-E<gt>bzflush($flush) ;>
  798.  
  799. Flushes all pending output to the compressed file.
  800. Works identically to the I<zlib> function it interfaces to. Note that
  801. the use of B<bzflush> can degrade compression.
  802.  
  803. Returns C<BZ_OK> if B<$flush> is C<BZ_FINISH> and all output could be
  804. flushed. Otherwise the bzlib error code is returned.
  805.  
  806. Refer to the I<bzlib> documentation for the valid values of B<$flush>.
  807.  
  808. =head2 B<$status = $bz-E<gt>bzeof() ;>
  809.  
  810. Returns 1 if the end of file has been detected while reading the input
  811. file, otherwise returns 0.
  812.  
  813. =head2 B<$bz-E<gt>bzclose>
  814.  
  815. Closes the compressed file. Any pending data is flushed to the file
  816. before it is closed.
  817.  
  818. =head2 B<$bz-E<gt>bzsetparams( [PARAMS] );>
  819.  
  820. Change settings for the deflate stream C<$bz>.
  821.  
  822. The list of the valid options is shown below. Options not specified
  823. will remain unchanged.
  824.  
  825. =over 5
  826.  
  827. =item B<-verbosity>
  828.  
  829. Defines the verbosity level. Valid values are 0 through 4,
  830.  
  831. The default is C<-verbosity =E<gt> 0>.
  832.  
  833. =item B<-blockSize100k>
  834.  
  835. For bzip object opened for stream deflation or write.
  836.  
  837. Defines the buffering factor of compression method.  The algorithm
  838. buffers all data until the buffer is full, then it flushes all the
  839. data out.  Use -blockSize100k to specify the size of the buffer.
  840.  
  841. Valid settings are 1 through 9, representing a blocking in multiples
  842. of 100k.
  843.  
  844. Note that each such block has an overhead of leading and trailing
  845. synchronization bytes.  bzip2 recovery uses this information to
  846. pull useable data out of a corrupted file.
  847.  
  848. A streaming application would probably want to set the blocking low.
  849.  
  850. =item B<-workFactor>
  851.  
  852. For bzip object opened for stream deflation or write.
  853.  
  854. The workFactor setting tells the deflation algorithm how much work
  855. to invest to compensate for repetitive data.
  856.  
  857. workFactor may be a number from 0 to 250 inclusive.  The default setting
  858. is 30.
  859.  
  860. See the bzip documentation for more information.
  861.  
  862. =item B<-small>
  863.  
  864. For bzip object opened for stream inflation or read.
  865.  
  866. B<small> may be 0 or 1.  Set C<small> to one to use a slower, less
  867. memory intensive algorithm.
  868.  
  869. =back
  870.  
  871. =head2 B<$bz-E<gt>bzerror>
  872.  
  873. Returns the I<bzlib> error message or number for the last operation
  874. associated with B<$bz>. The return value will be the I<bzlib> error
  875. number when used in a numeric context and the I<bzlib> error message
  876. when used in a string context. The I<bzlib> error number constants,
  877. shown below, are available for use.
  878.  
  879.   BZ_CONFIG_ERROR
  880.   BZ_DATA_ERROR
  881.   BZ_DATA_ERROR_MAGIC
  882.   BZ_FINISH
  883.   BZ_FINISH_OK
  884.   BZ_FLUSH
  885.   BZ_FLUSH_OK
  886.   BZ_IO_ERROR
  887.   BZ_MAX_UNUSED
  888.   BZ_MEM_ERROR
  889.   BZ_OK
  890.   BZ_OUTBUFF_FULL
  891.   BZ_PARAM_ERROR
  892.   BZ_RUN
  893.   BZ_RUN_OK
  894.   BZ_SEQUENCE_ERROR
  895.   BZ_STREAM_END
  896.   BZ_UNEXPECTED_EOF
  897.  
  898. =head2 B<$bzerrno>
  899.  
  900. The B<$bzerrno> scalar holds the error code associated with the most
  901. recent I<bzip2> routine. Note that unlike B<bzerror()>, the error is
  902. I<not> associated with a particular file.
  903.  
  904. As with B<bzerror()> it returns an error number in numeric context and
  905. an error message in string context. Unlike B<bzerror()> though, the
  906. error message will correspond to the I<bzlib> message when the error is
  907. associated with I<bzlib> itself, or the UNIX error message when it is
  908. not (i.e. I<bzlib> returned C<Z_ERRORNO>).
  909.  
  910. As there is an overlap between the error numbers used by I<bzlib> and
  911. UNIX, B<$bzerrno> should only be used to check for the presence of
  912. I<an> error in numeric context. Use B<bzerror()> to check for specific
  913. I<bzlib> errors. The I<bzcat> example below shows how the variable can
  914. be used safely.
  915.  
  916. =head1 Compress::Bzip2 1.03 COMPATIBILITY
  917.  
  918. While the 2.x thread forked off of 1.00, another line of development
  919. came to a head at 1.03.  The 1.03 version worked with bzlib 1.0.2, had
  920. improvements to the error handling, single buffer inflate/deflate, a
  921. streaming interface to inflate/deflate, and a cpan style test suite.
  922.  
  923. =head2 B<$dest = compress( $string, [$level] )>
  924.  
  925. Alias to memBzip, this compresses string, using the optional
  926. compression level, 1 through 9, the default being 1.  Returns a string
  927. containing the compressed data.
  928.  
  929. On error I<undef> is returned.
  930.  
  931. =head2 B<$dest = decompress($string)>
  932.  
  933. Alias to memBunzip, this decompresses the data in string, returning a
  934. string containing the decompressed data.
  935.  
  936. On error I<undef> is returned.
  937.  
  938. =head2 B<$stream = compress_init( [PARAMS] )>
  939.  
  940. Alias to bzdeflateInit.  In addition to the named parameters
  941. documented for bzdeflateInit, the following are accepted:
  942.  
  943.    -level, alias to -blockSize100k
  944.    -buffer, to set the buffer size.
  945.  
  946. The -buffer option is ignored.  The intermediate buffer size is not
  947. changeable.
  948.  
  949. =head2 B<$stream = decompress_init( [PARAMS] )>
  950.  
  951. Alias to bzinflateInit.  See bzinflateInit for a description of the parameters.
  952. The option "-buffer" is accepted, but ignored.
  953.  
  954. =head2 B<$output = $stream-E<gt>add( $string )>
  955.  
  956. Add data to be compressed/decompressed.  Returns whatever output is available
  957. (possibly none, if it's still buffering it), or undef on error.
  958.  
  959. =head2 B<$output = $stream-E<gt>finish( [$string] )>
  960.  
  961. Finish the operation; takes an optional final data string.  Whatever is
  962. returned completes the output; returns undef on error.
  963.  
  964. =head2 B<$stream-E<gt>error>
  965.  
  966. Like the function, but applies to the current object only.  Note that errors
  967. in a stream object are also returned by the function.
  968.  
  969. =head2 B<$stream-E<gt>input_size>
  970.  
  971. Alias to total_in.  Total bytes passed to the stream.
  972.  
  973. =head2 B<$stream-E<gt>output_size>
  974.  
  975. Alias to total_out.  Total bytes received from the stream.
  976.  
  977. =head1 GZIP COMPATIBILITY INTERFACE
  978.  
  979. Except for the exact state and error numbers, this package presents an
  980. interface very much like that given by the Compress::Zlib package.
  981. Mostly, if you take the method name, state or error number from
  982. Compress::Zlib and replace the "g" with a "b", your code should work.
  983.  
  984. To make the interoperability even easier, all the Compress::Zlib method
  985. names have been used as aliases or cover functions for the bzip2 methods.
  986.  
  987. Therefore, most code that uses Compress::Zlib should be able to use
  988. this package, with a one line change.
  989.  
  990. Simply change
  991.  
  992.    $gz = Compress::Zlib::gzopen( "filename", "w" );
  993.  
  994. to
  995.  
  996.    $gz = Compress::Bzip2::gzopen( "filename", "w" );
  997.  
  998. Some of the Compress::Zlib aliases don't return anything useful, like
  999. crc32 or adler32, cause bzip2 doesn't do that sort of thing.
  1000.  
  1001. =head2 B< $gz = gzopen( $filename, $mode ) >
  1002.  
  1003. Alias for bzopen.
  1004.  
  1005. =head2 B< $gz-E<gt>gzread( $buffer, [ $length ] ) >
  1006.  
  1007. Alias for bzread.
  1008.  
  1009. =head2 B< $gz-E<gt>gzreadline( $buffer ) >
  1010.  
  1011. Alias for bzreadline.
  1012.  
  1013. =head2 B< $gz-E<gt>gzwrite( $buffer ) >
  1014.  
  1015. Alias for bzwrite.
  1016.  
  1017. =head2 B< $gz-E<gt>gzflush( [$flushtype] ) >
  1018.  
  1019. Alias for bzflush, with return code translation.
  1020.  
  1021. =head2 B< $gz-E<gt>gzclose( ) >
  1022.  
  1023. Alias for bzclose.
  1024.  
  1025. =head2 B< $gz-E<gt>gzeof( ) >
  1026.  
  1027. Alias for bzeof.
  1028.  
  1029. =head2 B< $gz-E<gt>gzerror( ) >
  1030.  
  1031. Alias for bzerror.
  1032.  
  1033. =head2 B< $gz-E<gt>gzsetparams( $level, $strategy ) >
  1034.  
  1035. This is a no-op.
  1036.  
  1037. =head2 B< $d = deflateInit( [OPTS] ) >
  1038.  
  1039. Alias for bzdeflateInit, with return code translation.
  1040.  
  1041. All OPTS are ignored.
  1042.  
  1043. =head2 B< $d-E<gt>deflate( $buffer ) >
  1044.  
  1045. Alias for bzdeflate, with return code translation.
  1046.  
  1047. =head2 B< $d-E<gt>deflateParams( [OPTS] ) >
  1048.  
  1049. This is a no-op.
  1050.  
  1051. =head2 B< $d-E<gt>flush( [$flushtype] ) >
  1052.  
  1053. Cover function for bzflush or bzclose, depending on $flushtype.
  1054.  
  1055. See the Compress::Zlib documentation for more information.
  1056.  
  1057. =head2 B< $d-E<gt>dict_adler( ) >
  1058.  
  1059. This is a no-op.
  1060.  
  1061. =head2 B< $d-E<gt>msg( ) >
  1062.  
  1063. This is a no-op.
  1064.  
  1065. =head2 B< $d = inflateInit( [OPTS] ) >
  1066.  
  1067. Alias for bzinflateInit, with return code translation.
  1068.  
  1069. All OPTS are ignored.
  1070.  
  1071. =head2 B< $d-E<gt>inflate( ) >
  1072.  
  1073. Alias for bzinflate, with return code translation.
  1074.  
  1075. =head2 B< $d-E<gt>inflateSync( ) >
  1076.  
  1077. This is a no-op.
  1078.  
  1079. =head2 B< $d-E<gt>adler32( $crc ) >
  1080.  
  1081. This is a no-op.
  1082.  
  1083. =head2 B< $d-E<gt>crc32( $crc ) >
  1084.  
  1085. This is a no-op.
  1086.  
  1087. =head2 B< $buffer = memGzip( $buffer ) >
  1088.  
  1089. Alias for memBzip.
  1090.  
  1091. =head2 B< $buffer = memGunzip( $buffer ) >
  1092.  
  1093. Alias for memBunzip.
  1094.  
  1095. =head1 IN-MEMORY COMPRESS/UNCOMPRESS
  1096.  
  1097. Two high-level functions are provided by I<bzlib> to perform in-memory
  1098. compression. They are B<memBzip> and B<memBunzip>. Two Perl subs are
  1099. provided which provide similar functionality.
  1100.  
  1101. =head2 B<$compressed = memBzip($buffer);>
  1102.  
  1103. Compresses B<$source>. If successful it returns the compressed
  1104. data. Otherwise it returns I<undef>.
  1105.  
  1106. The buffer parameter can either be a scalar or a scalar reference.
  1107.  
  1108. Essentially, an in-memory bzip file is created. It creates a minimal
  1109. bzip header.
  1110.  
  1111. =head2 B<$uncompressed = memBunzip($buffer);>
  1112.  
  1113. Uncompresses B<$source>. If successful it returns the uncompressed
  1114. data. Otherwise it returns I<undef>.
  1115.  
  1116. The source buffer can either be a scalar or a scalar reference.
  1117.  
  1118. The buffer parameter can either be a scalar or a scalar reference. The
  1119. contents of the buffer parameter are destroyed after calling this
  1120. function.
  1121.  
  1122. =head1 STREAM DEFLATE 
  1123.  
  1124. The Perl interface will I<always> consume the complete input buffer
  1125. before returning. Also the output buffer returned will be
  1126. automatically grown to fit the amount of output available.
  1127.  
  1128. Here is a definition of the interface available:
  1129.  
  1130. =head2 B<($d, $status) = bzdeflateInit( [PARAMS] )>
  1131.  
  1132. Initialises a deflation stream. 
  1133.  
  1134. If successful, it will return the initialised deflation stream, B<$d>
  1135. and B<$status> of C<BZ_OK> in a list context. In scalar context it
  1136. returns the deflation stream, B<$d>, only.
  1137.  
  1138. If not successful, the returned deflation stream (B<$d>) will be
  1139. I<undef> and B<$status> will hold the exact I<bzip2> error code.
  1140.  
  1141. The function optionally takes a number of named options specified as
  1142. C<-Name=E<gt>value> pairs. This allows individual options to be
  1143. tailored without having to specify them all in the parameter list.
  1144.  
  1145. Here is a list of the valid options:
  1146.  
  1147. =over 5
  1148.  
  1149. =item B<-verbosity>
  1150.  
  1151. Defines the verbosity level. Valid values are 0 through 4,
  1152.  
  1153. The default is C<-verbosity =E<gt> 0>.
  1154.  
  1155. =item B<-blockSize100k>
  1156.  
  1157. Defines the buffering factor of compression method.  The algorithm
  1158. buffers all data until the buffer is full, then it flushes all the
  1159. data out.  Use -blockSize100k to specify the size of the buffer.
  1160.  
  1161. Valid settings are 1 through 9, representing a blocking in multiples
  1162. of 100k.
  1163.  
  1164. Note that each such block has an overhead of leading and trailing
  1165. synchronization bytes.  bzip2 recovery uses this information to
  1166. pull useable data out of a corrupted file.
  1167.  
  1168. A streaming application would probably want to set the blocking low.
  1169.  
  1170. =item B<-workFactor>
  1171.  
  1172. The workFactor setting tells the deflation algorithm how much work
  1173. to invest to compensate for repetitive data.
  1174.  
  1175. workFactor may be a number from 0 to 250 inclusive.  The default setting
  1176. is 30.
  1177.  
  1178. See the bzip documentation for more information.
  1179.  
  1180. =back
  1181.  
  1182. Here is an example of using the B<deflateInit> optional parameter list
  1183. to override the default buffer size and compression level. All other
  1184. options will take their default values.
  1185.  
  1186.     bzdeflateInit( -blockSize100k => 1, -verbosity => 1 );
  1187.  
  1188. =head2 B<($out, $status) = $d-E<gt>bzdeflate($buffer)>
  1189.  
  1190. Deflates the contents of B<$buffer>. The buffer can either be a scalar
  1191. or a scalar reference.  When finished, B<$buffer> will be
  1192. completely processed (assuming there were no errors). If the deflation
  1193. was successful it returns deflated output, B<$out>, and a status
  1194. value, B<$status>, of C<Z_OK>.
  1195.  
  1196. On error, B<$out> will be I<undef> and B<$status> will contain the
  1197. I<zlib> error code.
  1198.  
  1199. In a scalar context B<bzdeflate> will return B<$out> only.
  1200.  
  1201. As with the internal buffering of the I<deflate> function in I<bzip2>,
  1202. it is not necessarily the case that any output will be produced by
  1203. this method. So don't rely on the fact that B<$out> is empty for an
  1204. error test.  In fact, given the size of bzdeflates internal buffer,
  1205. with most files it's likely you won't see any output at all until
  1206. flush or close.
  1207.  
  1208. =head2 B<($out, $status) = $d-E<gt>bzflush([flush_type])>
  1209.  
  1210. Typically used to finish the deflation. Any pending output will be
  1211. returned via B<$out>.  B<$status> will have a value C<BZ_OK> if
  1212. successful.
  1213.  
  1214. In a scalar context B<bzflush> will return B<$out> only.
  1215.  
  1216. Note that flushing can seriously degrade the compression ratio, so it
  1217. should only be used to terminate a decompression (using C<BZ_FLUSH>) or
  1218. when you want to create a I<full flush point> (using C<BZ_FINISH>).
  1219.  
  1220. The allowable values for C<flush_type> are C<BZ_FLUSH> and C<BZ_FINISH>.
  1221.  
  1222. For a handle opened for "w" (bzwrite), the default is C<BZ_FLUSH>.
  1223. For a stream, the default for C<flush_type> is C<BZ_FINISH> (which is
  1224. essentially a close and reopen).
  1225.  
  1226. It is strongly recommended that you only set the C<flush_type>
  1227. parameter if you fully understand the implications of what it
  1228. does. See the C<bzip2> documentation for details.
  1229.  
  1230. =head2 Example
  1231.  
  1232. Here is a trivial example of using B<bzdeflate>. It simply reads standard
  1233. input, deflates it and writes it to standard output.
  1234.  
  1235.     use strict ;
  1236.     use warnings ;
  1237.  
  1238.     use Compress::Bzip2 ;
  1239.  
  1240.     binmode STDIN;
  1241.     binmode STDOUT;
  1242.     my $x = bzdeflateInit()
  1243.        or die "Cannot create a deflation stream\n" ;
  1244.  
  1245.     my ($output, $status) ;
  1246.     while (<>)
  1247.     {
  1248.         ($output, $status) = $x->bzdeflate($_) ;
  1249.     
  1250.         $status == BZ_OK
  1251.             or die "deflation failed\n" ;
  1252.     
  1253.         print $output ;
  1254.     }
  1255.     
  1256.     ($output, $status) = $x->bzclose() ;
  1257.     
  1258.     $status == BZ_OK
  1259.         or die "deflation failed\n" ;
  1260.     
  1261.     print $output ;
  1262.  
  1263. =head1 STREAM INFLATE
  1264.  
  1265. Here is a definition of the interface:
  1266.  
  1267. =head2 B<($i, $status) = inflateInit()>
  1268.  
  1269. Initialises an inflation stream. 
  1270.  
  1271. In a list context it returns the inflation stream, B<$i>, and the
  1272. I<zlib> status code (B<$status>). In a scalar context it returns the
  1273. inflation stream only.
  1274.  
  1275. If successful, B<$i> will hold the inflation stream and B<$status> will
  1276. be C<BZ_OK>.
  1277.  
  1278. If not successful, B<$i> will be I<undef> and B<$status> will hold the
  1279. I<bzlib.h> error code.
  1280.  
  1281. The function optionally takes a number of named options specified as
  1282. C<-Name=E<gt>value> pairs. This allows individual options to be
  1283. tailored without having to specify them all in the parameter list.
  1284.  
  1285. For backward compatibility, it is also possible to pass the parameters
  1286. as a reference to a hash containing the name=>value pairs.
  1287.  
  1288. The function takes one optional parameter, a reference to a hash.  The
  1289. contents of the hash allow the deflation interface to be tailored.
  1290.  
  1291. Here is a list of the valid options:
  1292.  
  1293. =over 5
  1294.  
  1295. =item B<-small>
  1296.  
  1297. B<small> may be 0 or 1.  Set C<small> to one to use a slower, less
  1298. memory intensive algorithm.
  1299.  
  1300. =item B<-verbosity>
  1301.  
  1302. Defines the verbosity level. Valid values are 0 through 4,
  1303.  
  1304. The default is C<-verbosity =E<gt> 0>.
  1305.  
  1306. =back
  1307.  
  1308. Here is an example of using the B<bzinflateInit> optional parameter.
  1309.  
  1310.     bzinflateInit( -small => 1, -verbosity => 1 );
  1311.  
  1312. =head2 B<($out, $status) = $i-E<gt>bzinflate($buffer)>
  1313.  
  1314. Inflates the complete contents of B<$buffer>. The buffer can either be
  1315. a scalar or a scalar reference.
  1316.  
  1317. Returns C<BZ_OK> if successful and C<BZ_STREAM_END> if the end of the
  1318. compressed data has been successfully reached.  If not successful,
  1319. B<$out> will be I<undef> and B<$status> will hold the I<bzlib> error
  1320. code.
  1321.  
  1322. The C<$buffer> parameter is modified by C<bzinflate>. On completion it
  1323. will contain what remains of the input buffer after inflation. This
  1324. means that C<$buffer> will be an empty string when the return status
  1325. is C<BZ_OK>. When the return status is C<BZ_STREAM_END> the C<$buffer>
  1326. parameter will contains what (if anything) was stored in the input
  1327. buffer after the deflated data stream.
  1328.  
  1329. This feature is useful when processing a file format that encapsulates
  1330. a compressed data stream.
  1331.  
  1332. =head2 Example
  1333.  
  1334. Here is an example of using B<bzinflate>.
  1335.  
  1336.     use strict ;
  1337.     use warnings ;
  1338.     
  1339.     use Compress::Bzip2;
  1340.     
  1341.     my $x = bzinflateInit()
  1342.        or die "Cannot create a inflation stream\n" ;
  1343.     
  1344.     my $input = '' ;
  1345.     binmode STDIN;
  1346.     binmode STDOUT;
  1347.     
  1348.     my ($output, $status) ;
  1349.     while (read(STDIN, $input, 4096))
  1350.     {
  1351.         ($output, $status) = $x->bzinflate(\$input) ;
  1352.     
  1353.         print $output 
  1354.             if $status == BZ_OK or $status == BZ_STREAM_END ;
  1355.     
  1356.         last if $status != BZ_OK ;
  1357.     }
  1358.     
  1359.     die "inflation failed\n"
  1360.         unless $status == BZ_STREAM_END ;
  1361.  
  1362. =head1 EXAMPLES
  1363.  
  1364. Here are some example scripts of using the interface.
  1365.  
  1366. =head2 B<A bzcat function>
  1367.  
  1368.   use strict ;
  1369.   use warnings ;
  1370.     
  1371.   use Compress::Bzip2 ;
  1372.     
  1373.   die "Usage: bzcat file...\n" unless @ARGV ;
  1374.     
  1375.   my $file ;
  1376.     
  1377.   foreach $file (@ARGV) {
  1378.     my $buffer ;
  1379.     
  1380.     my $bz = bzopen($file, "rb") 
  1381.        or die "Cannot open $file: $bzerrno\n" ;
  1382.     
  1383.     print $buffer while $bz->bzread($buffer) > 0 ;
  1384.     
  1385.     die "Error reading from $file: $bzerrno" . ($bzerrno+0) . "\n" 
  1386.        if $bzerrno != BZ_STREAM_END ;
  1387.         
  1388.     $bz->bzclose() ;
  1389.   }
  1390.  
  1391. =head2 B<A grep using bzreadline>
  1392.  
  1393.   use strict ;
  1394.   use warnings ;
  1395.     
  1396.   use Compress::Bzip2 ;
  1397.     
  1398.   die "Usage: bzgrep pattern file...\n" unless @ARGV >= 2;
  1399.     
  1400.   my $pattern = shift ;
  1401.     
  1402.   my $file ;
  1403.     
  1404.   foreach $file (@ARGV) {
  1405.     my $bz = bzopen($file, "rb") 
  1406.        or die "Cannot open $file: $bzerrno\n" ;
  1407.     
  1408.     while ($bz->bzreadline($_) > 0) {
  1409.       print if /$pattern/ ;
  1410.     }
  1411.     
  1412.     die "Error reading from $file: $bzerrno\n" 
  1413.       if $bzerrno != Z_STREAM_END ;
  1414.         
  1415.     $bz->bzclose() ;
  1416.   }
  1417.  
  1418. =head2 B<Streaming Compression>
  1419.  
  1420. This script, I<bzstream>, does the opposite of the I<bzcat> script
  1421. above. It reads from standard input and writes a bzip file to standard
  1422. output.
  1423.  
  1424.   use strict ;
  1425.   use warnings ;
  1426.     
  1427.   use Compress::Bzip2 ;
  1428.     
  1429.   binmode STDOUT;    # bzopen only sets it on the fd
  1430.     
  1431.   my $bz = bzopen(\*STDOUT, "wb")
  1432.      or die "Cannot open stdout: $bzerrno\n" ;
  1433.     
  1434.   while (<>) {
  1435.     $bz->bzwrite($_) or die "error writing: $bzerrno\n" ;
  1436.   }
  1437.  
  1438.   $bz->bzclose ;
  1439.  
  1440. =head1 EXPORT
  1441.  
  1442. Use the tags :all, :utilities, :constants, :bzip1 and :gzip.
  1443.  
  1444. =head2 Export tag :all
  1445.  
  1446. This exports all the exportable methods.
  1447.  
  1448. =head2 Export tag :constants
  1449.  
  1450. This exports only the BZ_* constants.
  1451.  
  1452. =head2 Export tag :bzip1
  1453.  
  1454. This exports the Compress::Bzip2 1.x functions, for compatibility.
  1455.  
  1456.    compress
  1457.    decompress
  1458.    compress_init
  1459.    decompress_init
  1460.    version
  1461.  
  1462. These are actually aliases to memBzip and memBunzip.
  1463.  
  1464. =head2 Export tag :utilities
  1465.  
  1466. This gives an interface to the bzip2 methods.
  1467.  
  1468.     bzopen
  1469.     bzinflateInit
  1470.     bzdeflateInit
  1471.     memBzip
  1472.     memBunzip
  1473.     bzip2
  1474.     bunzip2
  1475.     bzcat
  1476.     bzlibversion
  1477.     $bzerrno
  1478.  
  1479. =head2 Export tag :gzip
  1480.  
  1481. This gives compatibility with Compress::Zlib.
  1482.  
  1483.     gzopen
  1484.     gzinflateInit
  1485.     gzdeflateInit
  1486.     memGzip
  1487.     memGunzip
  1488.     $gzerrno
  1489.  
  1490. =head1 Exportable constants
  1491.  
  1492. All the I<bzlib> constants are automatically imported when you make use
  1493. of I<Compress::Bzip2>.
  1494.  
  1495.   BZ_CONFIG_ERROR
  1496.   BZ_DATA_ERROR
  1497.   BZ_DATA_ERROR_MAGIC
  1498.   BZ_FINISH
  1499.   BZ_FINISH_OK
  1500.   BZ_FLUSH
  1501.   BZ_FLUSH_OK
  1502.   BZ_IO_ERROR
  1503.   BZ_MAX_UNUSED
  1504.   BZ_MEM_ERROR
  1505.   BZ_OK
  1506.   BZ_OUTBUFF_FULL
  1507.   BZ_PARAM_ERROR
  1508.   BZ_RUN
  1509.   BZ_RUN_OK
  1510.   BZ_SEQUENCE_ERROR
  1511.   BZ_STREAM_END
  1512.   BZ_UNEXPECTED_EOF
  1513.  
  1514. =head1 SEE ALSO
  1515.  
  1516. The documentation for zlib, bzip2 and Compress::Zlib.
  1517.  
  1518. =head1 AUTHOR
  1519.  
  1520. Rob Janes, E<lt>arjay at cpan.orgE<gt>
  1521.  
  1522. =head1 COPYRIGHT AND LICENSE
  1523.  
  1524. Copyright (C) 2005 by Rob Janes
  1525.  
  1526. This library is free software; you can redistribute it and/or modify
  1527. it under the same terms as Perl itself, either Perl version 5.8.3 or,
  1528. at your option, any later version of Perl 5 you may have available.
  1529.  
  1530. =head1 AUTHOR
  1531.  
  1532. The I<Compress::Bzip2> module was originally written by Gawdi Azem
  1533. F<azemgi@rupert.informatik.uni-stuttgart.de>.
  1534.  
  1535. The first I<Compress::Bzip2> module was written by Gawdi Azem
  1536. F<azemgi@rupert.informatik.uni-stuttgart.de>.  It provided an
  1537. interface to the in memory inflate and deflate routines.
  1538.  
  1539. I<Compress::Bzip2> was subsequently passed on to Marco Carnut
  1540. F<kiko@tempest.com.br> who shepharded it through to version 1.03, a
  1541. set of changes which included upgrades to handle bzlib 1.0.2, and
  1542. improvements to the in memory inflate and deflate routines.  The
  1543. streaming interface and error information were added by David Robins
  1544. F<dbrobins@davidrobins.net>.
  1545.  
  1546. Version 2 of I<Compress::Bzip2> is due to Rob Janes, of
  1547. arjay@cpan.org.  This release is intended to give an interface
  1548. close to that of Compress::Zlib.  It's development forks from 1.00,
  1549. not 1.03, so the streaming interface is not the same as that in 1.03,
  1550. although apparently compatible as it passes the 1.03 test suite.
  1551.  
  1552. =head1 MODIFICATION HISTORY
  1553.  
  1554. See the Changes file.
  1555.  
  1556. 2.00 Second public release of I<Compress::Bzip2>.
  1557.  
  1558.  
  1559.  
  1560.